class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int start_x = 0, start_y = 0, offset = 1, count = 1;
        int i, j, k = n / 2;
        vector<vector<int>> nums(n, vector<int>(n)); 
        while (k--) {
            for (j = start_y; j < n - offset; j++)
                nums[start_x][j] = count++;
            for (i = start_x; i < n - offset; i++) 
                nums[i][j] = count++;
            for (; j > start_y; j--)
                nums[i][j] = count++;
            for (; i > start_x; i--)
                nums[i][j] = count++;
            start_x++;
            start_y++;
            offset++;
        }
        if (n % 2 != 0) {
            nums[start_x][start_y] = count;
        }
        return nums;
    }
};
